package com.gollum.common.core.utils;


import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.gollum.common.core.exception.ServiceException;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.UnsupportedEncodingException;
import java.util.Base64;


/**
 * 加密工具
 *
 * @author Herther
 * @version 1.0.0
 * @createTime 2022年09月05日 21:31:00
 */
public class EncrypUtils extends SecureUtil {

    /** 公钥 **/
    private static String publicKey;

    /** 私钥 **/
    private static String privateKey;

    /** RSA对象 **/
    private static RSA rsa;

    static
    {
        try {
            //读取私钥
            ClassPathResource keyResource =  new ClassPathResource ("encryp/private.key");
            privateKey =  IOUtils.toString(keyResource.getInputStream(), CharsetUtil.CHARSET_UTF_8);
            //读取公钥
            keyResource =  new ClassPathResource ("encryp/public.key");
            publicKey =  IOUtils.toString(keyResource.getInputStream(), CharsetUtil.CHARSET_UTF_8);
            //创建对象
            rsa = new RSA(privateKey,publicKey);
        }catch (Exception e){
            throw new ServiceException("读取密钥失败");
        }
    }

    /**
     * RSA 私钥解密
     *
     * @param  encrypt 解密字符串
     * @return 解密后的明文
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/5 21:44
     */
    public static String privateDecrypt(String encrypt){
        String plainStr = new String(rsa.decrypt(encrypt, KeyType.PrivateKey), CharsetUtil.CHARSET_UTF_8);
        return plainStr;
    }

    /**
     * RSA 公钥加密并且 转 Base 64
     *
     * @param str 需要加密的明文
     * @return 加密后的密文
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/5 21:45
     */
    public static String publicEncryptToBase64(String str){
        byte[] encrypt = rsa.encrypt(str, KeyType.PublicKey);
        return converToBase64(encrypt);
    }

    /**
     * 字节流转 base64
     *
     * @param encrypt 字节流
     * @return base64
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/5 22:05
     */
    public static String converToBase64(byte[] encrypt){
        return Base64.getEncoder().encodeToString(encrypt);
    }

    /**
     * 字符串转base64
     *
     * @param encrypt 需要转的字符串
     * @return base64字符串
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/5 22:11
     */
    public static String converToBase64(String encrypt) throws UnsupportedEncodingException {
        String base64;
        try {
            base64 = Base64.getEncoder().encodeToString(encrypt.getBytes(CharsetUtil.UTF_8));
        }catch (UnsupportedEncodingException e){
            throw new ServiceException("转换base64失败");
        }
        return base64;
    }

    /**
     * base64 转字符串
     *
     * @param base64
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/5 22:08
     */
    public static String base64ToString(String base64){
        byte[] decode = Base64.getDecoder().decode(base64);
        return new String(decode,CharsetUtil.CHARSET_UTF_8);
    }

    /**
     * RSA 私钥加密
     *
     * @param  str 需要加密的字符串
     * @return 解密后的明文
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/5 21:44
     */
    public static String privateEncrypt(String str){
        String plainStr = new String(rsa.encrypt(str, KeyType.PrivateKey), CharsetUtil.CHARSET_UTF_8);
        return plainStr;
    }

    /**
     * RSA 公钥解密
     *
     * @param  encrypt 解密字符串
     * @return 解密后的明文
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/5 21:44
     */
    public static String publicDecrypt(String encrypt){
        String plainStr = new String(rsa.decrypt(encrypt, KeyType.PublicKey), CharsetUtil.CHARSET_UTF_8);
        return plainStr;
    }

}
